<template>
	<view>
		<!-- 自定义头部导航 -->
		<!-- <page-meta>
			<navigation-bar :title="'原材料收货'" front-color="#000000" background-color="#FBFBFF" fixed />
		</page-meta> -->
		<view class="page">
			<view class="upload-scan">
				<scan-bar id="scanInput" ref="scanBarRef" placeholder="扫描包装条码" @code-scanned="getScanResult" :key="datekey"></scan-bar>
			</view>
			<view class="area-header">
				<u-text :text="'已扫描包装条码'+' (' + scanList.length+')'">
				</u-text>
			</view>
			<view class="list-content">
				<!-- 列表 -->
				<u-list class="list" v-if="scannedList && scannedList.length > 0" :height="ListViewHeight">
					<u-list-item v-for="(item, index) in scannedList" :key="item.id">
						<view class="card">
							<view class="right">
								<view class="title">
									包装条码: {{item.materialBarcode}}
								</view>
								<view class="body">
									供应商编号:{{ exteriorDelivery.supplierCode }}
								</view>
								<view class="body">
									供应商名称:{{ exteriorDelivery.supplierName }}
								</view>
								<view class="body">
									<label class="span">批次号:{{ item?.stockBatch || '待定'}}</label>
								</view>
								<view class="body">
									<label class="span">存货编码:{{ item.materialCode }}</label>
								</view>
								<view class="body">
									<label class="span">数量: {{item?.materialQuantity || '待定'}}</label>
								</view>
							</view>
							<view class="left">
								<u-icon v-if="item.isChecked == 1" name="checkmark-circle-fill" color="#42b20e"
									size="30px"></u-icon>
								<!-- <u-icon v-else name="close-circle-fill" color="#f56c6c" size="30px" @click="removeDetail(index)"></u-icon> -->
							</view>
						</view>

						<u-line></u-line>
					</u-list-item>
				</u-list>
				<!-- 空数据 -->
				<view v-else>
					<u-empty mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png"></u-empty>
				</view>
			</view>
			<u-line></u-line>
			<view class="search">
				<u-button type="primary" @click="save" class="custom-style" :throttleTime="10000">提交</u-button>
			</view>
		</view>
	</view>
</template>

<script setup lang="ts">
	import { computed, ref } from 'vue';
	import ScanBar from '@/components/scan-bar/index.vue';
	import { onShow, onPullDownRefresh } from '@dcloudio/uni-app';
	import { http } from "../../../utils/http";
	import { useUserStore } from '../../../stores/useUserStore';

	// 重置表单信息 & 光标自动blur
	const scanBarRef = ref();
	const ListViewHeight = ref(0);

	const exteriorDelivery = ref()
	const props = defineProps({
		result: {
			type: String,
		},
	});
	exteriorDelivery.value = JSON.parse(props.result)
	//已扫描集合
	const scanList = ref([]);
	const datekey = ref(new Date());
	const scannedList : any = ref([])
	onShow(async () => {
		ListViewHeight.value = uni.getSystemInfoSync().windowHeight - 160;
		console.log('ListViewHeight.value', ListViewHeight.value);
	})
	onPullDownRefresh(() => {
		scannedList.value = [];
		getUnit('refresh')
	});
	// 获取扫码结果
	async function getScanResult(result : string) {
		if (!result) {
			return
		}
		scanBarRef.value.reset();
		datekey.value = new Date();
		console.log('扫码结果', result);
		setScan(result)
	}
	getUnit()

	//查询明细信息
	async function getUnit(type ?: string) {
		const res = await http({
			method: 'POST',
			url: '/srm/srmexteriordeliveryunit/getUnitList',
			data: {
				exteriorDeliveryId: exteriorDelivery.value.id,
				isReceive: 0,
			}
		});
		if (res.data.length > 0) {
			scannedList.value = res.data
		} else {
			uni.$u.toast('未找到该包装！');
		}
		type === 'refresh' && uni.stopPullDownRefresh();
	}
	function setScan(result ?: string) {
		if (!result) {
			return
		}

		console.log('扫码结果', result);
		const index = scannedList.value.findIndex(item => item.materialBarcode === result);
		console.log(index)
		if (index == -1) {
			uni.$u.toast('请扫描正确的包装条码！');
			scanBarRef.value.reset();
		} else {
			const scannedStorageUnit = scanList.value.find(item => item.materialBarcode === result);
			console.log(scannedStorageUnit)
			if (scannedStorageUnit) {
				uni.$u.toast('这个包装已经被扫描过！');
			} else {
				scannedList.value[index].isChecked = 1;
				scanList.value.splice(0, 0, scannedList.value[index]);
			}
		}
	}
	//外协接收提交
	async function save() {
		if (scanList.value.length == 0) {
			uni.$u.toast('已扫包装不能为空！');
			return;
		}
		exteriorDelivery.value.units = scanList.value
		exteriorDelivery.value.unitsLength = scannedList.value.length
		await http({
			method: 'POST',
			url: '/wms/exterior/reception',
			data: exteriorDelivery.value
		}).then((res) => {
			if (res.code == "200") {
				uni.$u.toast('提交成功！');
				uni.navigateBack({
					delta: 1, //返回层数，2则上上页
				})
			}
		})
	}
</script>

<style lang="scss" scoped>
	.page {
		display: flex;
		flex-direction: column;
	}

	.upload-scan {
		padding: 10px 40rpx 10px 40rpx;
	}

	.area-header {
		color: #909193;
		padding: 10px 15px;
		margin-bottom: 0;
		font-size: 15px;
		background-color: #f3f4f6;
		display: flex;
	}

	.list-content {
		padding: $uni-page-padding;

		.list {
			.card {
				margin-top: 5px;
				margin-bottom: 10px;
				border-radius: 7px;
				display: flex;
				align-items: center;
				padding: 10px 0;
				transition: all 300ms ease;
				box-shadow: 0 0 1px rgba(0, 0, 0, 0.1);

				.left {
					// flex-basis: 90rpx;
					// padding-right: 10px;
					text-align: right;
					font-size: 13px;

					.enter {
						line-height: 20px;
						margin-bottom: 5px;
					}

					.number {
						font-size: 13px;
						line-height: 18px;
					}

					:deep(.u-icon--right) {
						flex-direction: row-reverse;
					}
				}

				.right {
					flex: 1;
					padding-left: 15px;

					.title {
						font-size: 18px;
						line-height: 20px;
						margin-bottom: 5px;
						font-weight: 700;
					}

					.body {
						font-size: 16px;
						line-height: 18px;

						.span {
							width: 50%;
							// text-align: left;
							display: inline-block;
						}
					}
				}
			}
		}
	}

	.search {
		display: flex;
		flex: 1;

		.custom-style {
			// width: 120px;
			margin: 12px 5px 5px;
			height: 40px;
			border-radius: 8px;

			&:nth-child(1) {
				flex: 1
			}

			&:nth-child(2) {
				flex: 3
			}
		}
	}
</style>